package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 汇款提醒设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:46
 */
public class PaymentRemindPushSet extends AbstractPushSet {

    private String pushObject;

    private String pushTime;

    private static final Integer AM08 = 8*3600;
    private static final Integer AM09 = 9*3600;
    private static final Integer AM17 = 17*3600;
    private static final Integer AM18 = 18*3600;

    private static final Integer AM08_TYPE = 1;
    private static final Integer AM09_TYPE = 2;
    private static final Integer AM17_TYPE = 3;
    private static final Integer AM18_TYPE = 4;

    private static  Integer oneDay = 86400;

    public String getPushObject() {
        return pushObject;
    }



    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    public String getPushTime() {
        return pushTime;
    }



    public void setPushTime(String pushTime) {
        this.pushTime = pushTime;
    }



    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,
                                         String attrEntity) {
        setPushObject(pushObject);
        setPushTime(pushTime);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,
                                    PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.PAYMENT_REMIND.getCode();

        Map<String,Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            if (Objects.equals(PushTypeEnum.PAYMENT_REMIND.getSubCode(), subType)) {
                pushSetEntity.setEnable(1);
            } else {
                pushSetEntity.setEnable(0);
            }
            JSONObject pushObject = new JSONObject();
             if (Objects.equals(PushTypeEnum.PAYMENT_REMIND.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.PAYMENT_REMIND.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_REMIND));
                //type
                pushObject.put("type", new JSONArray());
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
                //pushtime
                JSONObject pushTimeObject = new JSONObject();
                pushTimeObject.put("loop", 0);
                pushTimeObject.put("timeType", PushTimeTypeEnum.NOW.getCode());
                pushTimeObject.put("period", 0);
                pushTimeObject.put("time", PaymentRemindPushSet.AM08);
                pushTimeObject.put("type", 2);

                JSONArray timeArray = new JSONArray();
                JSONObject timeObject2 = new JSONObject();
                timeObject2.put("id", 3);
                timeObject2.put("name", I18nMessageUtil.getMessage(CommonConstant.THE_DAY));
                timeArray.add(timeObject2);
                pushTimeObject.put("timeArray", timeArray);

                pushSetEntity.setPushTime(pushTimeObject.toJSONString());
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            if (Objects.equals(PushTypeEnum.PAYMENT_REMIND.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.PAYMENT_REMIND.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_REMIND));
            }
        }
        return pushSetEntity;
    }



    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        Integer timeType = PushTimeTypeEnum.ADVANCE.getCode();
        Integer time = -1;
        Integer period = 0;
        Integer timeIntervalType = 0;
        Integer loop = 0;
        Integer pushTimeType = jsonObject.getInteger("pushTimeType");
        if(Objects.equals(pushTimeType, AM08_TYPE)){
            time = AM08;
        }else if(Objects.equals(pushTimeType, AM09_TYPE)){
            time = AM09;
        }else if(Objects.equals(pushTimeType, AM17_TYPE)){
            time = AM17;
        }else if(Objects.equals(pushTimeType, AM18_TYPE)){
            time = AM18;
        }
        JSONObject retObject = new JSONObject();
        retObject.put("timeType", timeType);
        retObject.put("time", time);
        retObject.put("period", period);
        retObject.put("timeIntervalType", timeIntervalType);
        retObject.put("loop", loop);
        return retObject;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

}
